package com.wocai.jrt.admin.user.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.security.core.GrantedAuthority;

import com.vteba.security.meta.DefaultGrantedAuthority;
import com.vteba.security.user.IUserDetails;
import com.vteba.security.user.Resource;
import com.vteba.service.generic.impl.MyBatisServiceImpl;
import com.vteba.tx.jdbc.mybatis.spi.BaseDao;
import com.wocai.jrt.admin.user.dao.ModuleMenuDao;
import com.wocai.jrt.admin.user.model.Authorities;
import com.wocai.jrt.admin.user.model.ModuleMenu;
import com.wocai.jrt.admin.user.model.ModuleMenuBean;
import com.wocai.jrt.admin.user.model.Resources;
import com.wocai.jrt.admin.user.service.spi.AuthoritiesService;
import com.wocai.jrt.admin.user.service.spi.ModuleMenuService;
import com.wocai.jrt.admin.user.service.spi.ResourcesService;
import com.wocai.jrt.orgadmin.utils.UserUtils;

/**
 * 菜单模块相关的service业务实现。
 * @author yinlei
 * @date 2015-7-6 11:50:56
 */
@Named
public class ModuleMenuServiceImpl extends MyBatisServiceImpl<ModuleMenu, ModuleMenuBean, String> implements ModuleMenuService {
	
	private ModuleMenuDao moduleMenuDao;
	
	@Inject
	private AuthoritiesService authoritiesServiceImpl;
	
	@Inject
	private ResourcesService resourcesServiceImpl;
	
	@Override
	@Inject
	public void setBaseDao(BaseDao<ModuleMenu, ModuleMenuBean, String> moduleMenuDao) {
		this.baseDao = moduleMenuDao;
		this.moduleMenuDao = (ModuleMenuDao) moduleMenuDao;
	}
	
	// 数据后面缓存
	public List<ModuleMenu> getModuleMenuList(IUserDetails user) {
		//获得所有的菜单
		ModuleMenu params = new ModuleMenu();
		params.setOrgId(user.getTenantId());
		params.setEnabled(1);
		params.setOrderBy("orders desc");
		List<ModuleMenu> menuList = moduleMenuDao.queryList(params);
		
		// 获取该用户所有的权限
		List<Authorities> authorities = authoritiesServiceImpl.queryAuthorityList(user.getId(), user.getTenantId());
		// spring security需要的数据
		Set<GrantedAuthority> authSet = new HashSet<GrantedAuthority>();
		
		// 防止出现空菜单
		List<ModuleMenu> resultMenuList = new ArrayList<ModuleMenu>();
		
		//匹配权限和菜单
		for (Authorities auth : authorities) {
			for (ModuleMenu menu : menuList) {
				if (auth.getModuleId().equals(menu.getModuleId())) {
					if (auth.getEnabled() == 1 && auth.getShowMenu()) { // 激活且不是隐式权限
						// 取得菜单下所有的资源，控制菜单显示
						Set<String> resUrls = new HashSet<String>();
						// 方法类型的资源
						Set<String> methodResourceSet = new HashSet<String>();
						
						List<Resources> resourceList = resourcesServiceImpl.queryResourceList(auth.getAuthId());
						
						for (Resources res : resourceList) {
							if (Resource.URL.equals(res.getResourceType())) {
								resUrls.add(res.getResourceUrl());
							} else if (Resource.METHOD.equals(res.getResourceType())) {
								methodResourceSet.add(res.getResourceUrl());
							}
						}
						// 权限的原始资源
						auth.setResourceList(resourceList);
						
						if (resUrls.size() > 0) {
							auth.setResUrls(resUrls);
						}
						if (methodResourceSet.size() > 0) {
							auth.setMethodResource(methodResourceSet);
						}
						// 用来控制菜单显示
						menu.getAuthorityList().add(auth);
					}
				}
			}
			// 构造权限，这个其实应该放到UserDetailService才更合理
			GrantedAuthority grantedAuthority = new DefaultGrantedAuthority(auth);
			authSet.add(grantedAuthority);
		}
		
		user.setAuthorities(authSet);
		user.setAuthorityList(authorities);
		
		for (ModuleMenu menu : menuList) {
			if (menu.getAuthorityList().size() > 0) {
				resultMenuList.add(menu);
			}
		}
		
		return resultMenuList;
	}

	public List<ModuleMenu> loadModuleList() {
		ModuleMenu params = new ModuleMenu();
		params.setEnabled(1);
		params.setOrgId(UserUtils.getOrgId());
		List<ModuleMenu> list = moduleMenuDao.queryList(params);
		return list;
	}
}
